#ifndef _CORE_H_INCLUDED_
#define _CORE_H_INCLUDED_
#include "doubleBufferedQueue.h"
#include "epollEngine.h"
#include "connection.h"
#include "peerConnection.h"
#include "core/log.h"
#include "lib/Socket.h"
#include <memory>


struct cycle_t {
    doubleBufferedQueue<epoll_event> queue;
    epollEngine IOMultiplexer;
    connectionPool connections;
    MyLogger logger;
    int serverSocket;
    peerConnection peers;
    std::condition_variable conn;
    std::mutex signal_lock;
    //cycle_t(doubleBufferedQueue<epoll_event>& a, epollEngine& b, connectionPool& c,int d, MyLogger& e):
    cycle_t(int serverSocket):
    queue(), IOMultiplexer(), peers(),connections(),serverSocket(serverSocket),logger(),conn(),signal_lock(){
        if(!peers.peers.empty()){
            std::list<connection_t*>::iterator it;
            for(it = peers.peers.begin(); it != peers.peers.end(); it++){
                epoll_event temp = IOMultiplexer.setEpollEvent((*it)->fd, EPOLLIN | EPOLLET, (void*)(*it)); //将各个连接加入epoll
                IOMultiplexer.epollAddEvent(temp, (*it)->fd);
                //std::cout << "将peer连接添加入epoll，fd为"<< (*it)->fd << std::endl;
            }
        }
    }
};

/*
echo: 向指定连接发送数据，类似php的echo
@return 
-1：发生错误
0：缓冲区已满，发送未完成，该发送事件被加入epoll中进行管理
>0：已发送的字节
*/
int echo(string& data, connection_t* conn, cycle_t& utils);

int peerSend(string& data, connection_t* conn, cycle_t& utils);

void closeConnection(connection_t* conn, cycle_t& utils);
#endif